package com.leetcode.LC;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;

public class LC1360 {//均未通过，因为力扣查无此类
    public int daysBetweenDates(String date1, String date2) {

        String[] split1 = date1.split("-");
        String[] split2 = date2.split("-");

        Calendar calendar = Calendar.getInstance();
        //这里需要注意一下,calendar传入的月份,是从0开始计数的(也就是说,2月相当于实际的三月)
        calendar.set(Integer.valueOf(split1[0]), Integer.valueOf(split1[1])-1, Integer.valueOf(split1[2]));
        long time1 = calendar.getTimeInMillis();
        calendar.set(Integer.valueOf(split2[0]), Integer.valueOf(split2[1])-1, Integer.valueOf(split2[2]));
        long time2 = calendar.getTimeInMillis();

        return (int) (Math.abs(time1-time2)/(60*60*24*1000));
    }

    public int daysBetweenDates2(String date1, String date2) {
        long time1=0,time2=0;
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        try {
            time1 = sdf.parse(date1).getTime();
            time2 = sdf.parse(date2).getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }

        return (int) (Math.abs(time1-time2)/(60*60*24*1000));
    }

    public static void main(String[] args) {
        System.out.println(new LC1360().daysBetweenDates2("2020-01-15", "2019-12-31"));
    }
}
